﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using kxcxw.Data.Entities;
using kxcxw.Data;
using System.Xml.Linq;
using System.Data;

namespace KXCXW.Web.CommonClass
{
    public class AdModel
    {
        public string id = "";
        public string name = "";
        public int width = 0;
        public int height = 0;
        public string src = "";
        public string href = "";
    }
    public class AdHelper
    {
        private KXCXWDBContext db = new KXCXWDBContext();
        private string shoptype = "";
        private string blockid = "";
        public string blockdesc = "";
        private string fileName = "";
        public static string ImgRootUrl = "Content/upload/Ad/";
        public AdHelper(string rootpath, string shoptype, string blockid)
        {
            fileName = rootpath;
            this.shoptype = shoptype;
            this.blockid = blockid;
        }
        public AdHelper(string rootpath)
        {
            fileName = rootpath;
        }
        public List<AdModel> getAdConfigList()
        {
            XmlHelper xmlh = new XmlHelper(fileName);
            XmlNodeList xnl = xmlh.GetSubNodes("AdConfig/ShopType");
            List<AdModel> list = new List<AdModel>();
            foreach (XmlNode xn in xnl)
            {
                if (shoptype == xn.Attributes["id"].Value)
                {
                    foreach (XmlNode xnn in xn.ChildNodes)
                    {
                        if ((shoptype + blockid) == xnn.Attributes["id"].Value)
                        {
                            blockdesc = xnn.Attributes["name"].Value;
                            int width = Convert.ToInt32(xnn.Attributes["width"].Value);
                            int height = Convert.ToInt32(xnn.Attributes["height"].Value);
                            foreach (XmlNode xnnn in xnn.ChildNodes)
                            {
                                AdModel ad = new AdModel();
                                ad.id = xnnn.Attributes["id"].Value;
                                ad.name = xnnn.Attributes["name"].Value;
                                ad.width = width;
                                ad.height = height;
                                list.Add(ad);
                            }
                            return list;
                        }
                    }
                }
            }
            return list;
        }

        public AdModel getAdModelByBlockId(string id)
        {
            AdApply ada = (from q in db.AdApplys
                           where q.AdBlockId == id && q.Status == 1 && System.Data.Objects.EntityFunctions.DiffDays(DateTime.Now, q.ApprovalDate) <= q.EffectiveDays
                           orderby q.ApprovalDate
                           select q).FirstOrDefault();
            if (ada != null)
            {
                AdModel adm = new AdModel();
                adm.id = id;
                adm.name = ada.AltText;
                adm.src = string.Format("~/{0}{1}", ImgRootUrl, ada.ImgName);
                adm.href = ada.Href;
                XDocument xd = XDocument.Load(fileName);
                XElement element = xd.Root;
                element = element.Descendants("block").Where(e => e.Attribute("id").Value == id.Substring(0, 2)).FirstOrDefault();
                if (element != null)
                {
                    adm.width = Convert.ToInt32(element.Attribute("width").Value);
                    adm.height = Convert.ToInt32(element.Attribute("height").Value);
                }
                return adm;
            }
            else
                return null;
        }
    }
}